import request from './request'
import type { PageResponse } from './request'

// 银行流水相关接口类型定义
export interface BankStatement {
  id?: number
  accountName: string
  bank: string
  bankFullName?: string
  bankTrxNumber?: string
  currency?: string
  transFlag: 'R' | 'P' // R-收入, P-支出
  transTypeName?: string
  customerBank?: string
  drAmount?: number
  crAmount?: number
  transAmount?: number
  trxDate: string
  cashType?: string
  balanceAmount?: number
  customerName?: string
  userMemo?: string
  uniqueNumber?: string
  createTime?: string
  updateTime?: string
}

export interface BankStatementSearchParams {
  pageNo: number
  pageSize: number
  bank?: string
  accountName?: string
  customerName?: string
  transFlag?: string
  trxDate?: string
  userMemo?: string
}

export interface UploadResult {
  filename: string
  success: boolean
  message: string
  data?: any
}

export interface ClassFieldProperty {
  name: string
  description: string
}

// 银行流水管理API
export const bankStatementApi = {
  // 分页查询银行流水列表
  getBankStatementPage(params: BankStatementSearchParams): Promise<PageResponse<BankStatement>> {
    return request.get('/bs/page', params)
  },

  // 根据ID获取银行流水详情
  getBankStatementById(id: number): Promise<BankStatement> {
    return request.get('/bs/get', { id })
  },

  // 创建银行流水
  createBankStatement(data: Omit<BankStatement, 'id'>): Promise<BankStatement> {
    return request.post('/bs/create', data)
  },

  // 更新银行流水
  updateBankStatement(data: BankStatement): Promise<BankStatement> {
    return request.put('/bs/update', data)
  },

  // 删除银行流水
  deleteBankStatement(id: number): Promise<void> {
    return request.delete('/bs/delete', { id })
  },

  // 批量删除银行流水
  batchDeleteBankStatement(ids: number[]): Promise<void> {
    return request.post('/bs/batch-delete', { ids })
  },

  // 上传银行流水文件
  uploadBankStatementFile(file: File): Promise<UploadResult> {
    const formData = new FormData()
    formData.append('file', file)
    return request.upload('/bs/upload', formData)
  },

  // 批量上传银行流水文件
  batchUploadBankStatementFiles(files: File[]): Promise<UploadResult[]> {
    const formData = new FormData()
    files.forEach(file => {
      formData.append('files', file)
    })
    return request.upload('/bs/batch-upload', formData)
  },

  // 导出Excel
  exportExcel(params: Omit<BankStatementSearchParams, 'pageNo' | 'pageSize'>): Promise<void> {
    return request.download('/bs/export-excel', params, 'bank-statements.xlsx')
  },

  // 获取统计数据
  getStatistics(params?: { 
    startDate?: string
    endDate?: string
    bank?: string
  }): Promise<{
    totalCount: number
    totalIncome: number
    totalExpense: number
    bankCount: number
  }> {
    return request.get('/bs/statistics', params)
  },

  getStatementStandardFields() : Promise<Array<ClassFieldProperty>> {
    return request.get('/bs/getStatementStandardFields')
  },

}